/**
 * 任务流程图相关API
 */
import { apiClient, API_ENDPOINTS } from './index'
import type { ApiResponse } from './types/common'

// 定义流程图相关类型
export interface FlowchartNode {
  id: string
  type: string
  label: string
  position: { x: number; y: number }
  data?: any
  style?: any
}

export interface FlowchartEdge {
  id: string
  source: string
  target: string
  type?: string
  label?: string
  data?: any
  style?: any
}

export interface TaskFlowchart {
  id: number
  title: string
  description: string
  season: string
  nodes_data: FlowchartNode[]
  edges_data: FlowchartEdge[]
  status: 'active' | 'inactive'
  sort_order: number
  create_time: string
  update_time: string
}

export interface FlowchartListResponse {
  list: TaskFlowchart[]
  seasons: string[]
}

export interface FlowchartSaveParams {
  title: string
  description?: string
  season: string
  nodes_data: FlowchartNode[]
  edges_data: FlowchartEdge[]
  status?: 'active' | 'inactive'
  sort_order?: number
}

export interface FlowchartUpdateParams extends Partial<FlowchartSaveParams> {
  id: number
}

// API 端点添加到 index.ts
export const FLOWCHART_ENDPOINTS = {
  LIST: '/taskFlowchart/index',
  DETAIL: '/taskFlowchart/detail',
  SAVE: '/taskFlowchart/save',
  UPDATE: '/taskFlowchart/update',
  VERIFY_EDIT_PASSWORD: '/taskFlowchart/verifyEditPassword'
}

/**
 * 获取流程图列表
 */
export function getFlowchartList(params?: { season?: string; status?: string }): Promise<ApiResponse<FlowchartListResponse>> {
  return apiClient.get(FLOWCHART_ENDPOINTS.LIST, { params })
}

/**
 * 获取流程图详情
 */
export function getFlowchartDetail(id: number): Promise<ApiResponse<TaskFlowchart>> {
  return apiClient.get(FLOWCHART_ENDPOINTS.DETAIL, { params: { id } })
}

/**
 * 保存新流程图
 */
export function saveFlowchart(data: FlowchartSaveParams): Promise<ApiResponse<{ id: number }>> {
  return apiClient.post(FLOWCHART_ENDPOINTS.SAVE, data)
}

/**
 * 更新流程图
 */
export function updateFlowchart(data: FlowchartUpdateParams): Promise<ApiResponse<any>> {
  return apiClient.post(FLOWCHART_ENDPOINTS.UPDATE, data)
}

/**
 * 验证编辑模式密码
 */
export function verifyEditPassword(password: string): Promise<ApiResponse<{ valid: boolean }>> {
  return apiClient.post(FLOWCHART_ENDPOINTS.VERIFY_EDIT_PASSWORD, { password })
}